//
//  WYCommentCell.m
//  Boss
//
//  Created by admin on 2018/6/8.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYCommentCell.h"
#import "WYReplayCell.h"
#import "WYReplyList.h"
@interface WYCommentCell()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *userImageView;

@property (strong, nonatomic) IBOutlet UILabel *userName;
@property (strong, nonatomic) IBOutlet UILabel *timeLabel;
@property (strong, nonatomic) IBOutlet UILabel *conntet;
@property (strong, nonatomic) IBOutlet UIButton *deleteBtn;
@property (strong, nonatomic) IBOutlet UIImageView *VIPImageView;

@property (strong, nonatomic) IBOutlet UITableView *replayTableView;
@property (strong, nonatomic) IBOutlet UIView *bgView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *bgViewTop;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *bgViewHeight;

@property(nonatomic,strong)UIButton *moreButton;
@end
@implementation WYCommentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self.userImageView setCornerRadius:20.f];
    self.replayTableView.delegate = self;
    self.replayTableView.dataSource = self;
    [self.replayTableView registerClass:[WYReplayCell class] forCellReuseIdentifier:@"WYReplayCell"];
    self.replayTableView.scrollEnabled = NO;
    self.replayTableView.backgroundColor = [UIColor clearColor];
    self.replayTableView.separatorColor=[UIColor clearColor];
    
    _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _moreButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [_moreButton setTitleColor:[UIColor hexStringToColor:@"#466db0" alpha:1.0] forState:UIControlStateNormal];
    _moreButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [_moreButton setTitle:@"共2条回复 >" forState:UIControlStateNormal];
    [_bgView addSubview:_moreButton];
    [_moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(_replayTableView);
        make.height.mas_equalTo(25);
        make.bottom.mas_equalTo(0);
    }];
    
    [_moreButton addTarget:self action:@selector(moreButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gogoHomePage)];
    
    
    [self.userImageView addGestureRecognizer:ges];
    
}
-(void)gogoHomePage
{
    if ([self.delegate respondsToSelector:@selector(clickImageViewWithUserId:)]) {
        [self.delegate clickImageViewWithUserId:self.list.userId];
    }
}


-(void)moreButtonClick:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(seeMoreComment:)]) {
        [self.delegate seeMoreComment:self.list];
    }
    NSLog(@"跳转更多回复");
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)setList:(WYCommentList *)list
{
    _list = list;
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:list.userPhoto] placeholderImage:[UIImage imageNamed:@"ic_defult_avatar"]];
    self.userName.text = list.userName;
    self.timeLabel.text = [NSString compareCurrentTime:list.createTime];
    self.conntet.text = list.content;
    [self.VIPImageView showVipImageViewStatusWithVipId:list.userVipLevel];
    WYUserMessage *userMessage = [WYStoreModule currentUserMessage];
    NSString *userID = userMessage.id;

    
    if (list.userId.integerValue==userID.integerValue) {
        self.deleteBtn.hidden = NO;
    }else{
        self.deleteBtn.hidden = YES;
    }
    CGFloat strHeight = 0.0;
    
    if (list.replyList.count > 0) {
        strHeight = 25 + 5*list.replyList.count;
        [_moreButton setTitle:[NSString stringWithFormat:@"共%@条回复 >",list.replyCount] forState:UIControlStateNormal];
    }else{
         strHeight = 0;
    }
    
    if (list.replyList.count) {
        self.bgViewTop.constant = 20;
        _bgView.hidden = NO;
        for (WYReplyList *model in list.replyList) {
             NSString *str = [NSString stringWithFormat:@"%@ 回复 %@: %@ ",model.fromUserName,model.toUserName,model.content];
            
           strHeight = strHeight +  [self contentHeight:str withWidth:[UIScreen mainScreen].bounds.size.width-20-78 withFont:13];
        }
         _bgViewHeight.constant = strHeight;
        
       
    }else{
        self.bgViewTop.constant = 0;
        _bgView.hidden = YES;
        _bgViewHeight.constant = 0;
    }
    
    
    [self.replayTableView reloadData];
    
    
    
    
    
    
    
    
    
    
}

-(CGFloat)contentHeight:(NSString *)content withWidth:(float)width withFont:(float)f
{
   
    CGRect rect=[content boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:f]} context:nil];
    return rect.size.height;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.replyList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYReplayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYReplayCell" forIndexPath:indexPath];
    WYReplyList *model = self.list.replyList[indexPath.row];
    //cell.replayLabel.text = model.content;
    [cell setModel:model];
    return cell;
}











- (IBAction)deleteAction:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(clickDeleteButtonWithCell:)]) {
        [self.delegate clickDeleteButtonWithCell:self];
    }
    
    
    
}








@end
